﻿using Learun.Util;
using Learun.Util.SqlSugar;
using System;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-06-21 15:32
    /// 描 述：端子排表
    /// </summary>
    public class ec_PanelStripService
    {
        #region 仓储
        Repository<ec_projectEntity> _projectRepository => new Repository<ec_projectEntity>();
        Repository<ec_PanelStripEntity> _panelStripRepository => new Repository<ec_PanelStripEntity>();
        #endregion
        #region 获取数据
        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ec_PanelStripEntity GetEntity(string ProjectId, string StripId)
        {
            try
            {
                var tableName = ProjectSugar.TableName<ec_PanelStripEntity>(ProjectId);
                return _panelStripRepository.AsQueryable().AS(tableName).First(x => x.StripID == StripId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string ProjectId, string keyValue)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                //关联类型删除时，删除工程数据关联表记录。
                var tableName = ProjectSugar.TableName<ec_PanelStripEntity>(ProjectId);
                var channelTableName = ProjectSugar.TableName<ec_PanelChannelEntity>(ProjectId);
                var termTableName = ProjectSugar.TableName<ec_PanelStripTermEntity>(ProjectId);
                _panelStripRepository.AsDeleteable().AS(tableName).Where(x => x.StripID == keyValue).ExecuteCommand();
                SqlSugarHelper.Db.Deleteable<ec_PanelChannelEntity>().AS(channelTableName).Where(x => x.StripID == keyValue).ExecuteCommand();
                SqlSugarHelper.Db.Deleteable<ec_PanelStripTermEntity>().AS(termTableName).Where(x => x.StripID == keyValue).ExecuteCommand();

                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }


        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string ProjectId, string keyValue, ec_PanelStripEntity entity, out string NewID)
        {
            NewID = "";
            try
            {
                var ProjectEntity = new ec_projectEntity();
                if (!string.IsNullOrWhiteSpace(ProjectId))
                {
                    ProjectEntity = _projectRepository.GetById(ProjectId);
                }

                if (!string.IsNullOrEmpty(keyValue))
                {
                    entity.Modify(keyValue);
                    if (string.IsNullOrWhiteSpace(ProjectId))
                    {
                        _panelStripRepository.Update(entity);
                    }
                    else
                    {
                        //this.BaseRepository().UpdateByNoMap(entity, $"ec_Panel_Strip_{ProjectEntity.ProjectIndex}", "StripID");
                        _panelStripRepository.AsUpdateable(entity).
                            IgnoreColumns(x => x.CreateTime).
                            IgnoreColumns(x => x.CreateUserID).
                            AS($"ec_Panel_Strip_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    }

                    NewID = keyValue;
                }
                else
                {
                    entity.Create();
                    //自动排序下
                    if (entity.Panel_Strip_Seq == null)
                    {
                        string strSql = $"select * from ec_Panel_Strip_{ProjectEntity.ProjectIndex} t where t.panelid='{entity.PanelID}' order by panel_strip_seq desc";
                        var StripWithMaxSeq = SqlSugarHelper.Db.Ado.SqlQuerySingle<ec_PanelStripEntity>(strSql);
                        if (StripWithMaxSeq != null)
                        {
                            entity.Panel_Strip_Seq = StripWithMaxSeq.Panel_Strip_Seq + 1;
                        }
                        else
                        {
                            entity.Panel_Strip_Seq = 0;
                        }
                    }
                    if (string.IsNullOrWhiteSpace(ProjectId))
                    {
                        _panelStripRepository.Insert(entity);
                    }
                    else
                    {
                        //this.BaseRepository().InsertByNoMap(entity, $"ec_Panel_Strip_{ProjectEntity.ProjectIndex}");
                        _panelStripRepository.AsInsertable(entity).AS($"ec_Panel_Strip_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    }
                    NewID = entity.StripID;
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        /// <summary>
        /// 调端子排所属Panel
        /// </summary>
        /// <param name="StripID">端子排ID</param>
        /// <param name="PanelID">PanelID</param>
        /// <param name="ProjectId">项目ID</param>
        public void SwitchPanelStrip(string StripID, string PanelID, string ProjectId)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                var tableName = ProjectSugar.TableName<ec_PanelStripEntity>(ProjectId);
                //var panelTableName = ProjectSugar.TableName<ec_PanelEntity>(ProjectId);
                var channelName = ProjectSugar.TableName<ec_PanelChannelEntity>(ProjectId);
                var termTableName = ProjectSugar.TableName<ec_PanelStripTermEntity>(ProjectId);
                var wireGroupTableName = ProjectSugar.TableName<ec_Wire_GroupEntity>(ProjectId);
                var wireTerminalTableName = ProjectSugar.TableName<ec_WireTerminalEntity>(ProjectId);

                SqlSugarHelper.Db.Updateable<ec_PanelStripEntity>().AS(tableName)
                    .SetColumns(x => x.PanelID == PanelID).Where(x => x.StripID == StripID).ExecuteCommand();

                //SqlSugarHelper.Db.Updateable<ec_PanelChannelEntity>().AS(channelName)
                //    .SetColumns(x => x.PanelID == PanelID).Where(x => x.StripID == StripID).ExecuteCommand();

                //SqlSugarHelper.Db.Updateable<ec_PanelStripTermEntity>().AS(termTableName)
                //    .SetColumns(x => x.PanelID == PanelID).Where(x => x.StripID == StripID).ExecuteCommand();

                //SqlSugarHelper.Db.Updateable<ec_Wire_GroupEntity>().AS(wireGroupTableName)
                //    .SetColumns(x => x.PanelID == PanelID).Where(x => x.StripID == StripID).ExecuteCommand();

                //SqlSugarHelper.Db.Updateable<ec_WireTerminalEntity>().AS(wireTerminalTableName)
                //    .SetColumns(x => x.PanelID == PanelID).Where(x => x.StripID == StripID).ExecuteCommand();

                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        #endregion

    }
}
